package com.youxin.busfoundation.common.redis;

import org.springframework.beans.BeanUtils;
import com.youxin.busfoundation.bean.login.RequestLoginVerifyCode;
import com.youxin.busfoundation.bean.login.UserInfo;
import com.youxin.busfoundation.common.GlobalConfig;
import com.youxin.foundation.db.redis.RedisClusterCommand;

/**
 * @desc Redis操作
 * @author yingde.cao
 * @date 2017-12-16
 */
public class RedisOperator {
	/**
	 * 登录验证码设置
	 * @param bean 手机号码+验证码
	 */
	public static String saveLoginVerifyCode(RequestLoginVerifyCode bean){
		String key=RedisKeyGenerater.getLoginVerifyCode(bean.getMobile());		
		return RedisClusterCommand.instance().getRedisCluster().setex(key, GlobalConfig.LOGIN_VERIFY_CODE_EXPIRES_SECONDS, bean.getCode());
	}
	
	/**
	 * 获取登录验证码剩余时间
	 * @param mobile
	 * @return
	 */
	public static Long retainSecondsForLoginCode(String mobile)
	{
		String key=RedisKeyGenerater.getLoginVerifyCode(mobile);
		return RedisClusterCommand.instance().getRedisCluster().ttl(key);
	}
	
	/**
	 * 是否能够再次获取登录验证码
	 * @param mobile
	 * @return
	 */
	public static boolean isCanGetLoginCode(String mobile)
	{
		Long secondsObj=retainSecondsForLoginCode(mobile);
		if(secondsObj==null) {
			return true;
		}
		long seconds=secondsObj.intValue();
		if(seconds<(GlobalConfig.LOGIN_VERIFY_CODE_EXPIRES_SECONDS-GlobalConfig.VERIFY_CODE_INTERVAL))
		{
			return true;
		}
		return false;
	}
	
	/**
	 * 获取登录验证码
	 * @param mobile 手机号码
	 * @return String
	 */
	public static String getLoginVerifyCode(String mobile){
		String key=RedisKeyGenerater.getLoginVerifyCode(mobile);		
		return RedisClusterCommand.get(key);
	}
	
	public static Long removeLoginVerifyCode(String mobile){
		String key=RedisKeyGenerater.getLoginVerifyCode(mobile);		
		return RedisClusterCommand.delete(key);
	}
	
	/**
	 * 首页试用保存验证码
	 * @param bean 手机号码+验证码
	 */
	public static String saveTryoutVerifyCode(RequestLoginVerifyCode bean){
		String key=RedisKeyGenerater.getRegisterCodeForTryOut(bean.getMobile());		
		return RedisClusterCommand.instance().getRedisCluster().setex(key, GlobalConfig.LOGIN_VERIFY_CODE_EXPIRES_SECONDS, bean.getCode());
	}
	/**
	 * 获取试用验证码剩余时间
	 * @param mobile
	 * @return
	 */
	public static Long retainSecondsForTryoutCode(String mobile)
	{
		String key=RedisKeyGenerater.getRegisterCodeForTryOut(mobile);
		return RedisClusterCommand.instance().getRedisCluster().ttl(key);
	}
	/**
	 * 是否能够再次获取试用验证码
	 * @param mobile
	 * @return
	 */
	public static boolean isCanGetTryoutCode(String mobile)
	{
		Long secondsObj=retainSecondsForTryoutCode(mobile);
		if(secondsObj==null) {
			return true;
		}
		long seconds=secondsObj.intValue();
		if(seconds<(GlobalConfig.LOGIN_VERIFY_CODE_EXPIRES_SECONDS-GlobalConfig.VERIFY_CODE_INTERVAL))
		{
			return true;
		}
		return false;
	}
	
	/**
	 * 首页试用获取登录验证码
	 * @param mobile 手机号码
	 * @return String
	 */
	public static String getTryoutVerifyCode(String mobile){
		String key=RedisKeyGenerater.getRegisterCodeForTryOut(mobile);		
		return RedisClusterCommand.get(key);
	}
	
	public static Long removeTryoutVerifyCode(String mobile){
		String key=RedisKeyGenerater.getRegisterCodeForTryOut(mobile);		
		return RedisClusterCommand.delete(key);
	}
	
	public static String saveAlipay(long id,String form)
	{
		String key=RedisKeyGenerater.getAliPayKey(id);
		return RedisClusterCommand.instance().getRedisCluster().setex(key, 3600*24, form);
	}
	
	public static String getAliPay(long id)
	{
		String key=RedisKeyGenerater.getAliPayKey(id);
		return RedisClusterCommand.instance().getRedisCluster().get(key);
	}
	
	/**
	 * 保存MemberInfo
	 * @param userInfo
	 * @return String
	 */
	public static String saveUserInfo(UserInfo userInfo)
	{
		String key=RedisKeyGenerater.getMemberInfoKey(userInfo.getId());
		return RedisClusterCommand.setObject(key, userInfo);
	}
	/**
	 * 移除用户信息
	 * @param userId
	 * @return
	 */
	public static Long removeUserInfo(long userId)
	{
		String key=RedisKeyGenerater.getMemberInfoKey(userId);
		if(RedisClusterCommand.exists(key))
		{
			return -1L;
		}
		return RedisClusterCommand.delete(key);
	}
	
	/**
	 * 保存UserInfo
	 * @param source TbMemberInfo对象
	 * @return String
	 */
	public static String saveUserInfo(Object source)
	{
		UserInfo userInfo=new UserInfo();
		BeanUtils.copyProperties(source, userInfo);
		return RedisOperator.saveUserInfo(userInfo);		
	}
	
	/**
	 * 获取UserInfo
	 * @param id
	 * @return Object
	 */
	public static UserInfo getUserInfo(long id)
	{
		String key=RedisKeyGenerater.getMemberInfoKey(id);
		return RedisClusterCommand.getObject(key, UserInfo.class);
	}

	/**
	 * @desc 获取订单支付状态
	 * @param orderSn
	 * @return String
	 */
	public static String getOrderPayStatus(String orderSn)
	{
		String key=RedisKeyGenerater.getOrderPayStatusKey(orderSn);
		return RedisClusterCommand.get(key);
	}

	/**
	 * @desc 保存订单支付状态
	 * @param orderSn 手机号码+验证码
	 */
	public static String saveOrderPayStatus(String orderSn)
	{
		String key=RedisKeyGenerater.getOrderPayStatusKey(orderSn);
		return RedisClusterCommand.instance().getRedisCluster().setex(key, GlobalConfig.LOGIN_VERIFY_CODE_EXPIRES_SECONDS, orderSn);
	}
	
	/**
	 * 获取旧的绑定
	 * @param entMemberId
	 * @param mobile
	 * @return
	 */
	public static String getOldCallBind(long entMemberId) {
		try {
			String key=RedisKeyGenerater.getCallBindKey(entMemberId);
			return RedisClusterCommand.instance().getRedisCluster().get(key);
		} catch (Exception e) {
			return null;
		}		
	}
	
	public static String saveCallBind(long entMemberId,String subid,int seconds) {
		try {
			String key=RedisKeyGenerater.getCallBindKey(entMemberId);
			return RedisClusterCommand.instance().getRedisCluster().setex(key, GlobalConfig.LOGIN_VERIFY_CODE_EXPIRES_SECONDS, subid);

		} catch (Exception e) {
			return null;
		}
	}

	
}
